package cn.com.taiji.oospub.wechat.common;

import cn.com.taiji.oospub.wechat.common.tool.TimeTools;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.util.Calendar;

/**
 * 将Calendar转换为JSON字符串
 * 
 * @author allen-Taiji
 * 
 */
public class CalendarValueProcessor implements JsonValueProcessor {

	private String format = TimeTools.DEFAULT_FORMAT;

	public CalendarValueProcessor() {
	}

	public CalendarValueProcessor(String format) {
		setFormat(format);
	}

	public String getFormat() {
		return format;
	}

	private void setFormat(String format) {
		this.format = format;
	}

	@Override
	public Object processArrayValue(Object value, JsonConfig config) {
		return process(value);
	}

	@Override
	public Object processObjectValue(String key, Object value, JsonConfig config) {
		return process(value);
	}

	protected Object process(Object value) {
		if (value == null || !(value instanceof Calendar)) {
            return "";
        }
		return TimeTools.calendar2String((Calendar) value, getFormat());
	}

}
